package org.fall.system.controller.backstage;

import org.fall.system.annotation.AdminRoleCheckAnnotation;
import org.fall.system.model.ResultEntity;
import org.fall.system.model.dto.AdminUserDTO;
import org.fall.system.model.dto.UserQueryDTO;
import org.fall.system.model.vo.AdminUserVO;
import org.fall.system.service.BackstageAdminUserService;
import org.fall.system.service.BackstageIndexService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjh
 * @since 2021/4/1 22:03
 */
@Controller
@RequestMapping("/admin/user/")
public class BackstageAdminUserController {

    @Resource
    private BackstageAdminUserService backstageAdminUserService;

    @Resource
    private BackstageIndexService backstageIndexService;

    /**
     * 查询指定条件的管理员用户列表
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param keyword 关键词
     * @param modelMap modelMap
     * @return 跳转list.html页面
     */
    @GetMapping("list")
    @AdminRoleCheckAnnotation(AdminRoleCheckAnnotation.LEVEL_ONE)
    public String getAdminUsers(@RequestParam(value = "startDate", defaultValue = "") String startDate,
                                @RequestParam(value = "endDate", defaultValue = "") String endDate,
                                @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                ModelMap modelMap) {
        UserQueryDTO dto = new UserQueryDTO(startDate, endDate, keyword);
        List<AdminUserVO> list = backstageAdminUserService.queryAdminUserList(dto);
        modelMap.addAttribute("adminUsers", list);
        return "pages/admin/list";
    }

    /**
     * 根据username删除管理员账号
     * @param username username
     * @return 删除结果
     */
    @PostMapping("delete")
    @ResponseBody
    @AdminRoleCheckAnnotation(AdminRoleCheckAnnotation.LEVEL_THREE)
    public ResultEntity deleteAdminUser(@RequestParam("username") String username) {
        return backstageAdminUserService.deleteAdminUser(username);
    }

    /**
     * 批量删除
     * @param list 要删除的id的list
     * @return 删除结果
     */
    @PostMapping("batch/delete")
    @ResponseBody
    @AdminRoleCheckAnnotation(AdminRoleCheckAnnotation.LEVEL_THREE)
    public ResultEntity batchDeleteAdminUsers(@RequestBody List<Integer> list) {
        return backstageAdminUserService.batchDeleteAdminUsers(list);
    }

    /**
     * 修改管理员用户的状态
     * @param username 修改状态的用户username
     * @return 修改结果
     */
    @PostMapping("changeState")
    @ResponseBody
    @AdminRoleCheckAnnotation(AdminRoleCheckAnnotation.LEVEL_TWO)
    public ResultEntity changeAdminUserState(@RequestParam("username") String username) {
        return backstageAdminUserService.changeAdminUserState(username);
    }

    /**
     * 打开指定id的管理员的修改页面
     * @param id 修改的管理员的id
     * @param modelMap modelMap
     * @return 修改页面地址
     */
    @GetMapping("edit/page")
    public String getEditPage(@RequestParam("id") Integer id, ModelMap modelMap) {
        AdminUserVO adminUserVo = backstageAdminUserService.getAdminUserById(id);
        modelMap.put("adminUser", adminUserVo);
        return "pages/admin/edit";
    }

    /**
     * 执行修改
     * @param dto dto
     * @return 修改结果
     */
    @PostMapping("edit")
    @ResponseBody
    @AdminRoleCheckAnnotation(AdminRoleCheckAnnotation.LEVEL_THREE)
    public ResultEntity editAdminUser(@RequestBody AdminUserDTO dto) {
        return backstageAdminUserService.editAdminUser(dto);
    }


}
